﻿using System;
using System.Text;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using com.dz.components;
using com.dz.common;
namespace com.dz.web
{
    public partial class ChapterList :BasePage
    {
        public string m_book_name = string.Empty;
        string m_author_name = string.Empty;
        DataSet m_ds_book = null;
        BookManager m_book_mgr = new BookManager();
        public long m_book_id = 0;
        const string c_volume_template = "<div class=\"tit3\"><span class=\"f12 fw_n\">{0}</span></div>";
        const string c_chapter_container = "<div id=\"yekacont12\" class=\"bd\"><div class=\"clear\"><table class=\"list-table\"><tbody>{0}</tbody></table></div><div class=\"cb\"></div></div>";
        const string c_chapter = "<td class=\"cc\"><A title=\"字数：{0}  更新时间：{1}\" href=\"/chapter/{2},{3}.aspx\" target=\"_blank\" class=\"graylink\">{4}</A></td>";
        public string m_book_category_name = string.Empty;
        public string m_book_category_id = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!long.TryParse(Request["bookId"], out m_book_id))
                m_book_id = 0;
            if (m_book_id == 0)
            {
                RedirectErrorPage("请求无效");
            }
            if (Token != null)
            {
                lblNickName.Text = string.Format("<script>var nickName='{0}';</script>", Token.NickName);
            }
            BindData();
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {

            Page.Title = string.Format("{0}最新章节列表,尽在{1},免费下载,在线阅读", m_book_name,AppConfig.SiteName);

            metaDescription.Content =
                string.Format("{3}提供{0}全文免费阅读、{0}最新章节和列表,免费{0}电子书、{0}TXT下载。看{0}最新章节就到{0}免费小说{3}。{2}小说《{0}》为{0}作者{1}作品",
                    m_book_name, m_author_name, m_book_category_name,AppConfig.SiteName);
            metaKeyWords.Content = m_book_name + ",最新,最新章节," + m_author_name + ",免费" ;

        }

        /// <summary>
        /// 绑定书籍信息
        /// </summary>
        private void BindData()
        {
            try
            {
                m_ds_book = m_book_mgr.GetBookChapterList(m_book_id);
                DateTime t_last_update_time = DateTime.Now;
                if (m_ds_book != null && m_ds_book.Tables.Count > 0)
                {
                    DataTable i_dt_book = m_ds_book.Tables[0];
                    //lblAuthor.Text = i_dt_book.Rows[0]["authorName"].ToString();
                    //lblBookName.Text = i_dt_book.Rows[0]["bookName"].ToString();
                    //lblDescription.Text = i_dt_book.Rows[0]["description"].ToString();
                    //if (!DateTime.TryParse(i_dt_book.Rows[0]["lastUpdateTime"].ToString(), out t_last_update_time))
                    //    t_last_update_time = DateTime.Now;
                    //lblLastUpdateTime.Text = t_last_update_time.ToString("dd HH:mm:ss");
                    //lblSubCategoryName.Text = i_dt_book.Rows[0]["bookSubCategoryName"].ToString();
                    //lblWordsCount.Text = i_dt_book.Rows[0]["wordscount"].ToString();
                    m_book_category_id = i_dt_book.Rows[0]["bookCategoryId"].ToString();
                    m_book_name = i_dt_book.Rows[0]["bookName"].ToString();
                    m_book_category_name = i_dt_book.Rows[0]["booksubcategoryName"].ToString();
                    m_author_name = i_dt_book.Rows[0]["authorname"].ToString();
                    if (m_ds_book.Tables.Count == 3)
                    {
                        BindBookChapter(m_ds_book.Tables[1], m_ds_book.Tables[2]);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("绑定书籍介绍和章节列表失败" + ex.ToString());
            }
        }



        /// <summary>
        /// 绑定书籍章节
        /// </summary>
        /// <param name="i_dt_volume"></param>
        /// <param name="i_dt_chapter"></param>
        private void BindBookChapter(DataTable i_dt_volume, DataTable i_dt_chapter)
        {
            StringBuilder sb = new StringBuilder();

            if (i_dt_chapter == null || i_dt_chapter.Rows.Count <= 0)
                return;
            try
            {
                if (i_dt_volume != null && i_dt_volume.Rows.Count > 0)
                {


                    for (int index = 0; index < i_dt_volume.Rows.Count; index++)
                    {
                        sb.AppendFormat(c_volume_template,SystemUtil.FilterTitle( i_dt_volume.Rows[index]["volumeName"].ToString()), "");

                        DataRow[] t_chapter = i_dt_chapter.Select(string.Format("volumeName='{0}'", i_dt_volume.Rows[index]["volumeName"]));
                        StringBuilder sb_chapter = new StringBuilder();
                        if (t_chapter != null && t_chapter.Length > 0)
                        {
                            sb_chapter.Append("<tr>");
                            for (int index_chapter = 0; index_chapter < t_chapter.Length; index_chapter++)
                            {
                                if (index_chapter % 6 == 0)
                                {
                                    sb_chapter.Append("</tr>");
                                }
                                sb_chapter.AppendFormat(c_chapter, t_chapter[index_chapter]["wordscount"], t_chapter[index_chapter]["updatetime"], t_chapter[index_chapter]["bookId"], t_chapter[index_chapter]["chapterId"], SystemUtil.GetGBString( SystemUtil.FilterTitle( t_chapter[index_chapter]["chapterName"].ToString()),40));
                            }

                            sb_chapter.Append("</tr>");


                        }
                        sb.AppendFormat(c_chapter_container, sb_chapter.ToString());
                        sb_chapter = new StringBuilder();
                    }
                }
                else
                {
                    StringBuilder sb_chapter = new StringBuilder();
                    sb_chapter.Append("<tr>");
                    for (int index = 0; index < i_dt_chapter.Rows.Count; index++)
                    {
                        if (index % 20 == 0)
                        {
                            sb.AppendFormat(c_volume_template, string.Format("第{0}卷", (index + 1), ""));
                            sb.AppendFormat(c_chapter_container, sb_chapter.ToString());
                            sb_chapter = new StringBuilder();
                        }
                        if (index % 6 == 0)
                        {
                            sb_chapter.Append("</tr>");
                        }

                        sb_chapter.AppendFormat(c_chapter, i_dt_chapter.Rows[index]["wordscount"], i_dt_chapter.Rows[index]["updatetime"], i_dt_chapter.Rows[index]["bookId"], i_dt_chapter.Rows[index]["chapterId"], SystemUtil.FilterTitle( i_dt_chapter.Rows[index]["chapterName"].ToString()));

                    }
                    sb_chapter.Append("</tr>");
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("绑定章节失败" + ex.ToString());
            }

            lblChapter.Text = sb.ToString();
        }
    }
}
